﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using servermon_library;

namespace servermon
{
	public partial class AddServerWindow : Form
	{
		ServerManager manager;
		Main main;
		public AddServerWindow(ServerManager _manager, Main mainwindow)
		{
			InitializeComponent();
			manager = _manager;
			main = mainwindow;
		}

		private void txtAdd_Click(object sender, EventArgs e)
		{
			if (txtName.Text != "" && txtAdress.Text != "")
			{
				if (regIP.IsMatch(txtAdress.Text))
				{
					Server server = new Server(0, txtName.Text, txtAdress.Text);
					manager.AddServer(server);
					main.window_Update();
					this.Close();
				}
				else
				{
					MessageBox.Show("IP address is invalid!");
				}
			}
			else
			{
				MessageBox.Show("Please fill in both fields!");
			}
		}
		// regex for ip address validation
		private static Regex regIP = new Regex(@"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.
    ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$",
			RegexOptions.IgnoreCase
			| RegexOptions.CultureInvariant
			| RegexOptions.IgnorePatternWhitespace
			| RegexOptions.Compiled
		);
	}
}
